package com.qkl.xiatang.app.dto.device;

import com.qkl.xiatang.app.utils.AppUtils;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * 传减器数据统计,输入
 */
@Data
public class SensorStatisticsInDTO {

    private static final String STAT_TYPE_HOUR = "1";
    private static final String STAT_TYPE_DAY = "2";

    private static final String SQL_HOUR = "%Y-%m-%d-%H";
    private static final String SQL_DAY = "%Y-%m-%d";

    private String companyId;

    private Integer sensorType;

    private String sensorId;

    private String fieldName;

    private Date startDate;

    private Date endDate;

    private String sn;

    /**
     * 统计类型 : 1 按小时,2 按日
     */
    private String statisticsType;

    /**
     * sql日期格式
     */
    private String sqlDateFormat;

    public boolean checkStatisticsType(){
        return statisticsTypeHour() || statisticsTypeDay();
    }

    public boolean statisticsTypeHour(){
        boolean b = STAT_TYPE_HOUR.equals(statisticsType);
        if(b){
            sqlDateFormat = SQL_HOUR;
        }
        return b;
    }

    public boolean statisticsTypeDay(){
        boolean b = STAT_TYPE_DAY.equals(statisticsType);
        if(b){
            sqlDateFormat = SQL_DAY;
        }
        return b;
    }


    public static SensorStatisticsInDTO parseParams(String sensorTypeId,String startTime,String endTime){
        SensorStatisticsInDTO dto = new SensorStatisticsInDTO();
        String[] arr = sensorTypeId.split("-");
        if(null != arr && arr.length > 1){
            dto.setSensorId(arr[0]);
            dto.setSensorType(AppUtils.parseInt(arr[1]));
        }
        startTime = AppUtils.toYYYY_MM_DD(startTime);
        endTime = AppUtils.toYYYY_MM_DD(endTime);

        Date startDate = null;
        Date endDate = null;

        if(StringUtils.isNotEmpty(startTime)){
            startDate = AppUtils.yyyyMMddToDate(startTime);
        }
        if(StringUtils.isNotEmpty(endTime)){
            endDate = AppUtils.yyyyMMddToDate(endTime);
        }

        if(null == startDate && null == endDate){
            startDate = AppUtils.todayYyyyMMdd();
            endDate = AppUtils.todayYyyyMMdd();
        }
        if(null != endDate){
            endDate = AppUtils.addDays(endDate,1);
        }

        dto.setStartDate(startDate);
        dto.setEndDate(endDate);
        return dto;
    }

}
